package com.shuwang.goods.controller;

import com.shuwang.goods.domain.GoodsCollectionRequest;
import com.shuwang.goods.exception.ConflictException;
import com.shuwang.goods.exception.ForbiddenException;
import com.shuwang.goods.exception.NotFoundException;
import com.shuwang.goods.mapper.GoodsCollectionMapper;
import com.shuwang.goods.model.GoodsCollection;
import com.shuwang.goods.model.GoodsCollectionExample;
import com.shuwang.goods.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2017/6/5.
 */
@RestController
@RequestMapping(value = "goods")
@Api(value = "goodsCollection",description = "录入商品收藏信息")
public class GoodsCollectionController {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private GoodsCollectionMapper goodsCollectionMapper;

    @RequestMapping(value = "goodsrelative/collection", method = RequestMethod.POST)
    @ApiOperation(value = "商品收藏信息接口")
    public List<GoodsCollection> createGoodsCollection(@ApiParam(value = "商品收藏信息实体类") @RequestBody GoodsCollectionRequest request) {

        GoodsCollectionExample goodsCollectionExample =new GoodsCollectionExample();
        goodsCollectionExample.createCriteria().andGoodsIdEqualTo(request.getGoodsId()).andUserIdEqualTo(request.getUserId());
        if(!goodsCollectionMapper.selectByExample(goodsCollectionExample).isEmpty()){
            log.debug("该商品分类关联信息已存在");
            throw new ConflictException("该商品分类关联信息已存在");
        }

        GoodsCollection goodsCollection = new GoodsCollection();
        goodsCollection.setCollectTime(new Date());
        Long collectionId = IDBuilder.CreateAnyId("111");
        goodsCollection.setCollectionId(collectionId);
        BeanUtils.copyProperties(request,goodsCollection);
        goodsCollectionMapper.insertSelective(goodsCollection);

        List<GoodsCollection> success = goodsCollectionMapper.selectByExample(goodsCollectionExample);
        if(success.isEmpty()){
            log.debug("商品收藏信息入库失败");
            throw new NotFoundException("商品收藏信息入库失败");
        }
        return success;
    }

    @RequestMapping(value = "goods/getcollection/{collectionId}",method = {RequestMethod.GET})
    @ApiOperation(value = "根据categoryId查询商品收藏信息", httpMethod = "", response = GoodsCollection.class)
    public List<GoodsCollection> getGoodsCollectionByCollectionIdController(@ApiParam(value = "请输入collectionId") @PathVariable Long collectionId ) {

        GoodsCollectionExample goodsCollectionExample =new GoodsCollectionExample();
        goodsCollectionExample.createCriteria().andCollectionIdEqualTo(collectionId);
        List<GoodsCollection> goodscollection =goodsCollectionMapper.selectByExample(goodsCollectionExample);

        if( goodscollection.isEmpty()){
            log.debug("该商品收藏信息不存在");
            throw new NotFoundException("该商品收藏信息不存在");
        }
        return goodscollection;
    }

    @RequestMapping(value = "goods/deletecollection/{collectionId}",method = {RequestMethod.DELETE})
    @ApiOperation(value = "根据collectionId删除商品收藏信息", httpMethod = "DELETE", response = GoodsCollection.class)
    public List<GoodsCollection>  deleteGoodsCollectionByCollectionIdController(@ApiParam(value = "collectionId") @PathVariable Long collectionId ) {

        GoodsCollectionExample goodsCollectionExample = new GoodsCollectionExample();
        goodsCollectionExample.createCriteria().andCollectionIdEqualTo(collectionId);
        List<GoodsCollection> goodscollection =goodsCollectionMapper.selectByExample(goodsCollectionExample);
        if (goodscollection.isEmpty()) {
            log.debug("该商品收藏信息不存在");
            throw new NotFoundException("该商品收藏信息不存在");
        }
        goodsCollectionMapper.deleteByExample(goodsCollectionExample);

        if (!goodsCollectionMapper.selectByExample(goodsCollectionExample).isEmpty()) {
            log.debug("该商品收藏信息删除失败");
            throw new ForbiddenException("该商品收藏信息删除失败");

        }
        return goodscollection;
    }
}
